#include "myswitch.h"
#include <stdio.h>

static void switch_1(void)
{
    lv_obj_t* switch1 = lv_switch_create(lv_scr_act());

    //改变主体颜色
    // lv_obj_set_style_bg_color(switch1, lv_color_hex(0xdf5345), LV_PART_MAIN);

    //改变手柄颜色
    lv_obj_set_style_bg_color(switch1, lv_color_hex(0xdf5345), LV_PART_KNOB);

    //改变指示器颜色，不加LV_STATE_CHECKED没有效果
    lv_obj_set_style_bg_color(switch1, lv_color_hex(0xdf5345), LV_STATE_CHECKED|LV_PART_INDICATOR);
}

#if 0
static void switch_2(void)
{
    //添加状态：默认打开且不可修改
    lv_obj_add_state(switch1, LV_STATE_CHECKED | LV_STATE_DISABLED);

    //清除开关的状态
    lv_obj_clear_state(switch1, LV_STATE_CHECKED | LV_STATE_DISABLED);
}
#endif

#if 0
lv_obj_t* switch1;

static void event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_VALUE_CHANGED)
    {
        if(lv_obj_has_state(switch1, LV_STATE_CHECKED))
            printf("LED ON\n");
        else
            printf("LED OFF\n");
    }
}

//主程序
static  void switch_3(void)
{
    switch1 = lv_switch_create(lv_scr_act());
    lv_obj_add_event_cb(switch1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}
#endif


lv_obj_t* label;
lv_obj_t* switchCool;
lv_obj_t* switchHeart;
lv_obj_t* switchDry;

//定义速度显示标签
static void lv_label(void)
{
    label = lv_label_create(lv_scr_act());
    lv_label_set_text_fmt(label, "Control Center");
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align(label, LV_ALIGN_CENTER,0,-200);
}

//事件处理函数
static void event_cb(lv_event_t* e)
{
    lv_obj_t* target = lv_event_get_target(e);
    if(target == switchCool)
    {
        if(lv_obj_has_state(switchCool, LV_STATE_CHECKED))
            lv_obj_clear_state(switchHeart,LV_STATE_CHECKED);
    }
    else if(target == switchHeart)
    {
        if(lv_obj_has_state(switchHeart, LV_STATE_CHECKED))
            lv_obj_clear_state(switchCool,LV_STATE_CHECKED);
    }
}

//制冷
static void lv_switch_Cool(void)
{
    //创建背景图
    lv_obj_t* menu1 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(menu1, 150,150);
    lv_obj_align(menu1, LV_ALIGN_LEFT_MID,40,0);

    //制冷标签
    lv_obj_t* labelCool = lv_label_create(menu1);
    lv_label_set_text_fmt(labelCool, "Cool");
    lv_obj_set_style_text_font(labelCool, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(labelCool, LV_ALIGN_TOP_MID);

    //开关
    switchCool = lv_switch_create(menu1);
    lv_obj_set_size(switchCool, 100, 50);
    lv_obj_align(switchCool, LV_ALIGN_CENTER,0,10);

    lv_obj_add_event_cb(switchCool, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

//制热
static void lv_switch_Heat(void)
{
    //创建背景图
    lv_obj_t* menu2 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(menu2, 150,150);
    lv_obj_set_align(menu2, LV_ALIGN_CENTER);

    //制热标签
    lv_obj_t* labelHeart = lv_label_create(menu2);
    lv_label_set_text_fmt(labelHeart, "Heart");
    lv_obj_set_style_text_font(labelHeart, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(labelHeart, LV_ALIGN_TOP_MID);

    //开关
    switchHeart = lv_switch_create(menu2);
    lv_obj_set_size(switchHeart, 100, 50);
    lv_obj_align(switchHeart, LV_ALIGN_CENTER,0,10);

    lv_obj_add_event_cb(switchHeart, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

//干燥
static void lv_switch_Dry(void)
{
    //创建背景图
    lv_obj_t* menu3 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(menu3, 150,150);
    lv_obj_align(menu3, LV_ALIGN_RIGHT_MID,-40,0);

    //干燥标签
    lv_obj_t* labelDry = lv_label_create(menu3);
    lv_label_set_text_fmt(labelDry, "Dry");
    lv_obj_set_style_text_font(labelDry, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(labelDry, LV_ALIGN_TOP_MID);

    //开关
    switchDry = lv_switch_create(menu3);
    lv_obj_set_size(switchDry, 100, 50);
    lv_obj_align(switchDry, LV_ALIGN_CENTER,0,10);
    // 该开关默认打开，不可点击
    lv_obj_add_state(switchDry, LV_STATE_DEFAULT | LV_STATE_DISABLED);
    // 该开关默认关闭，不可点击
    // lv_obj_add_state(switchDry, LV_STATE_DEFAULT | LV_STATE_DISABLED);    
}

//主程序
static void switch_4(void)
{
    lv_label();
    lv_switch_Cool();
    lv_switch_Heat();
    lv_switch_Dry();
}


void myswitch_example(void)
{
    switch_4();
}